﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Palsta.Core
{
    /*
     * Factory for creating XslElement Node on runtime
     */ 
    public class XslElementFactory
    {
        /// <summary>
        /// this function is used to build the declaration of js function according to the xsl element entity
        /// <param name="Node">Original Xml node, use it to create xsl element object accordingly</param>
        /// </summary>
        public static XslElement CreateInstance(XmlNode Node)
        {
            String nameOfElement = Node.Name.ToLower();
            XslElement xslNode = null;
            switch (nameOfElement)
            {
                case "xsl:if":
                    xslNode = new XslIf(Node);
                    break;
                case "xsl:choose":
                    xslNode = new XslChoose(Node);
                    break;
                case "xsl:when":
                    xslNode = new XslWhen(Node);
                    break;
                case "xsl:value-of":
                    xslNode = new XslValueOf(Node);
                    break;
                case "xsl:attribute":
                    xslNode = new XslAttribute(Node);
                    break;
                // add other nodes on above
            }
            // The text node is a special one but we need to convert it into js function as well if need
            if (xslNode == null && Node.NodeType == XmlNodeType.Text && Node.ParentNode.Name.ToLower().IndexOf("xsl:") != -1)
            {
                xslNode = new XslText(Node);
            }
            // normal html node
            if (xslNode == null && Node.Name.ToLower().IndexOf("xsl:") == -1)
            {
                xslNode = new XslHTML(Node);
            }
            return xslNode;
        }
    }
}
